/**
 * \file installer/main.c
 * \author Davide Gessa
 * \date 10-27-2009
 */
#include <stdio.h>
#include <ncurses.h>
#include <menu.h>
#include <malloc.h>

#define WHITEONRED 1
#define WHITEONBLUE 2
#define WHITEONBLACK 3
#define BLACKONWHITE 4
	
	
	int runMenu(char *choices[])
	{
	ITEM **my_items;	/* list of items on this menu */
	int c;			/* key pressed */	
	MENU *my_menu;		/* the menu structure */
        int n_choices;		/* number of items on menu */
        int ssChoice;		/* subscript to run around the choices array */
	int my_choice = -1;	/* the zero based numeric user choice */
	
	
	/* CALCULATE NUMBER OF MENU CHOICES */
	for(n_choices=0; choices[n_choices]; n_choices++);

	/* ALLOCATE ITEM ARRAY AND INDIVIDUAL ITEMS */
        my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *));
        for(ssChoice = 0; ssChoice < n_choices; ++ssChoice)
                my_items[ssChoice] = new_item(choices[ssChoice], NULL);
	my_items[n_choices] = (ITEM *)NULL;

	/* CREATE THE MENU STRUCTURE AND DISPLAY IT */
	my_menu = new_menu((ITEM **)my_items);
	post_menu(my_menu);
	refresh();

	/* HANDLE USER KEYSTROKES */
	while(my_choice == -1)
		{
		c = getch();
		switch(c)
			{
			case KEY_DOWN:
				menu_driver(my_menu, REQ_DOWN_ITEM);
				break;
			case KEY_UP:
				menu_driver(my_menu, REQ_UP_ITEM);
				break;
			case 10:	/* Enter */
				my_choice = item_index(current_item(my_menu));

				/* RESET CURSOR IN CASE MORE SELECTION IS NECESSARY */
				refresh(); 
				pos_menu_cursor(my_menu);
				break;
			}
		}	

	/* FREE ALL ALLOCATED RESOURCES */
	free_item(my_items[0]);
        free_item(my_items[1]);
	free_menu(my_menu);

	/* RETURN THE ZERO BASED NUMERIC USER CHOICE */
	return(my_choice);
	}
void winfill(WINDOW * pwin)
{
	int y, x, maxy, maxx;
	getmaxyx(pwin, maxy, maxx);
	for(y=0; y < maxy; y++)
		for(x=0; x < maxx; x++)
			mvwaddch(pwin, y, x, ' ');
}

int main_menu()
{
	
	
}

/**
 * Installer con interfaccia in curses
 */
int main(int argc, char **argv)
{
	// Inizializzazioni
	initscr();
	char *menu_list[] =
	{
			"Partitions",
			"Kernel select",
			"Package select",
			"Install",
			NULL
	};
	int menu_choice;
	int menu_which = 1;	
	cbreak();	
	noecho();
	keypad(stdscr, TRUE);
	
	
	WINDOW *base_win;
	int maxy, maxx;

	/* INITIALIZE CURSES AND COLORS AND REFRESH THE STANDARD SCREEN */
	initscr();
	getmaxyx(stdscr, maxy, maxx);
	start_color();
	init_pair(WHITEONRED, COLOR_WHITE, COLOR_RED);
	init_pair(WHITEONBLUE, COLOR_WHITE, COLOR_BLUE);
	init_pair(WHITEONBLACK, COLOR_WHITE, COLOR_BLACK);
	init_pair(BLACKONWHITE, COLOR_BLACK, COLOR_WHITE);
	wrefresh(stdscr);
	
	/* CREATE AND DISPLAY THE BASE WINDOW */
	base_win = newwin(maxy-maxy/5, maxx-maxx/10, maxy/10, maxx/20);
	wattrset(base_win, COLOR_PAIR(WHITEONBLUE) | WA_BOLD);
	winfill(base_win);
	box(base_win, 0, 0);
	//mvwaddstr(base_win, 0, 2, "Main menu");
	touchwin(base_win);
	wrefresh(base_win);
	getch();



	// Menu' principale
	while(1)
	{
		menu_choice = runMenu(menu_list);
	}
	
	
	// Fine
	endwin();
	
	return 0;
}
